import store from '@/store'

export default {
    inserted (el, binding) {
        const { value } = binding
        const powers = store.state && store.state.permissions
        if (value && value instanceof Array && value.length > 0) {
            const permission = value
            const hasPermission = powers.some(power => {
                return permission.includes(power)
            })

            if (!hasPermission) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        } else {
            throw new Error('使用方式： v-permission="[\'admin\',\'editor\']"')
        }
    },
    isPermission (value) {
        const powers = store.state && store.state.permissions
        if (value && value instanceof Array && value.length > 0) {
            const permission = value
            const hasPermission = powers.some(power => {
                return permission.includes(power)
            })
            return hasPermission
        }else {
            throw new Error('使用方式： this.permission("edit"])')
        }
    }
}
